package com.hmjk.health.views;

import com.hmjk.health.R;
import com.hmjk.health.util.LOCATION_INFO;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CardAddressRoom extends LinearLayout{
	private Context mContext;
	private LinearLayout mWaitView;
	private LinearLayout mEmptyView;
	private boolean blue = true;
	private LinearLayout mDetailView;
	
	public CardAddressRoom(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public CardAddressRoom(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public CardAddressRoom(Context context) {
		super(context);
		init(context);
	}
	
	private void init(Context context){
		mContext = context;
		LayoutInflater inflater = LayoutInflater.from(context);
		mWaitView = (LinearLayout)inflater.inflate(R.layout.card_addr_wait_view, null);
		mEmptyView = (LinearLayout)inflater.inflate(R.layout.card_addr_empty_view, null);
		mDetailView = (LinearLayout)inflater.inflate(R.layout.card_addr_detail_view, null);
		addView(mWaitView, 0);
	}
	
	public void setLoading(boolean show){
		if(show){
			mWaitView.setVisibility(View.VISIBLE);
		}else{
			mWaitView.setVisibility(View.GONE);
		}
	}
	
	public void clearAll(){
		int count = getChildCount();
		if(count > 1)
			removeViews(1, count-1);
		
		blue = true;
	}
	
	public void checkEmpty(){
		int count = getChildCount();
		if(count < 2)
			addView(mEmptyView);
	}
	
	public void hasMore(View.OnClickListener listener){
		if(listener != null){
		addView(mDetailView);
		mDetailView.setOnClickListener(listener);
		}
	}
	
	public void addDateView(String date){
		LayoutInflater inflater = LayoutInflater.from(mContext);
		RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.card_addr_time_view, null);
		TextView datetext = (TextView) view.findViewById(R.id.date);
		
		if(date.equalsIgnoreCase("today")){
			datetext.setText(R.string.addr_today);
		}else if(date.equalsIgnoreCase("yesterday")){
			datetext.setText(R.string.addr_yesterday);
		}else{
			datetext.setText(date);
		}
		addView(view);
	}
	
	public void addAddressView(String timestr, String address, int flag){
		LayoutInflater inflater = LayoutInflater.from(mContext);
		LinearLayout view = (LinearLayout) inflater.inflate(R.layout.card_addr_addr_view, null);
		TextView timetext = (TextView) view.findViewById(R.id.time);
		timetext.setText(timestr);
		LinearLayout addrView = (LinearLayout) view.findViewById(R.id.addr_view);
		TextView addrtext = (TextView) view.findViewById(R.id.address);
		addrtext.setText(address);
		ImageView icon = (ImageView) view.findViewById(R.id.icon);
		ImageView sos = (ImageView) view.findViewById(R.id.sos);
		
		if((flag & LOCATION_INFO.FLAG_SOS) == LOCATION_INFO.FLAG_SOS){
			sos.setVisibility(View.VISIBLE);
		}else{
			sos.setVisibility(View.GONE);
		}
		if(blue){
			icon.setImageResource(R.drawable.addr_blue);
			addrView.setBackgroundResource(R.drawable.addr_blue_bg);
		}else{
			icon.setImageResource(R.drawable.addr_yellow);
			addrView.setBackgroundResource(R.drawable.addr_yellow_bg);
		}
		blue = !blue;
		addView(view);
	}
}
